//
//  LzwFourDetailVC.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/7.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwFourDetailVC.h"
#import "Block.h"
#import "LzwFourDetailCollectCell.h"
#import "LzwFourDetailWebVC.h"
#import "LzwbaseCollectView.h"

@interface LzwFourDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) LzwbaseCollectView *myFourCollectView;

@end

@implementation LzwFourDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"物品屋";
    
    
    UIImage * LandRightBar = [UIImage imageNamed:@"return.png"];
    LandRightBar = [LandRightBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LandRightBar style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
    
    UICollectionViewFlowLayout *myFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    myFlowLayout.itemSize = CGSizeMake(160 * WIDTH, 180 * WIDTH);
    
    myFlowLayout.sectionInset = UIEdgeInsetsMake(15 * WIDTH, 17 * WIDTH, 10 * WIDTH, 17 * WIDTH);
    
    myFlowLayout.minimumInteritemSpacing = 20 * WIDTH;
    myFlowLayout.minimumLineSpacing = 20 * WIDTH;
    
    myFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.myFourCollectView = [[LzwbaseCollectView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:myFlowLayout];
    self.myFourCollectView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myFourCollectView];
 
    self.myFourCollectView.delegate = self;
    
    self.myFourCollectView.dataSource = self;
    
    self.arrayFour = [NSMutableArray array];
    
    [self.myFourCollectView registerClass:[LzwFourDetailCollectCell class] forCellWithReuseIdentifier:@"cell"];
    
    NSString *string = @"http://open3.bantangapp.com/topic/list?app_installtime=1451365479.933367&app_versions=5.1.1&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=c02c60075e7bab110fbdf27e15de3cb61e97360c4ea3545fd8a5936e46c28ccb&ids=1807%2C1719%2C1612%2C1370%2C1450%2C1316&oauth_token=a41d3848f43102aa7759c27ba2c0fe8f&os_versions=9.1&page=0&pagesize=20&screensize=640&track_device_info=iPhone5%2C3&track_deviceid=A80585E3-D6AD-4A53-BE1C-5963901C96E5&track_user_id=1746436&v=9";
    
    
    [Block creatingDataTask:string parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
       
        NSMutableDictionary *dicF = [resuposeObject objectForKey:@"data"];
        NSMutableArray *arrayF = [dicF objectForKey:@"topic"];
        
        for (NSMutableDictionary *dic in arrayF) {
            LzwFourDeModle *modleF = [[LzwFourDeModle alloc]init];
            
            [modleF setValuesForKeysWithDictionary:dic];
            
            [self.arrayFour addObject:modleF];
            
            NSLog(@"eeeeeeee = %ld",self.arrayFour.count);
        }
        
        [self.myFourCollectView reloadData];
        
    }];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayFour.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwFourDetailCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.modleF = [self.arrayFour objectAtIndex:indexPath.row];
    
    return cell;
}

//点击item触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwFourDetailWebVC *fourWebVc = [[LzwFourDetailWebVC alloc]init];
    fourWebVc.modleW = [self.arrayFour objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:fourWebVc animated:YES];
}

- (void)leftButtonAction:(UIBarButtonItem *)bat{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
